package com.calvin.lingting.utils;

import com.calvin.lib.json.JSONArray;
import com.calvin.lib.json.JSONObject;
import com.calvin.lib.utils.HTTPUtils;
import com.calvin.lingting.config.LrtsConfig;

import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LrtsUtils {

	public static JSONObject getCategory() {
		String url = String.format("%s/%s/", LrtsConfig.URL, LrtsConfig.URL_CATEGORY);
		String data = LrtsUtils.getURLData(url, LrtsConfig.ENCODING);
		JSONObject categoryMap = new JSONObject();
		Matcher matcher = Pattern.compile(LrtsConfig.REG_CATEGORY).matcher(data);
		while (matcher.find()) {
			String subUrl = String.format("%s%s", url, matcher.group(1));
			String name = matcher.group(2);
			String subData = LrtsUtils.getURLData(subUrl, LrtsConfig.ENCODING);
			JSONObject subCategoryMap = new JSONObject();
			Matcher subMatcher = Pattern.compile(LrtsConfig.REG_SUBCATEGORY).matcher(subData);
			while (subMatcher.find()) {
				subCategoryMap.put(subMatcher.group(2), subMatcher.group(1));
			}
			categoryMap.put(name, subCategoryMap);
		}
		return categoryMap;
	}

	public static JSONArray getBook(String category, String type, int page) {
		String url = String.format("%s/%s/%s/%s/%s/20", LrtsConfig.URL, LrtsConfig.URL_CATEGORY, category, type, page);
		String data = LrtsUtils.getURLData(url, LrtsConfig.ENCODING);
		JSONArray bookList = new JSONArray();
		Matcher matcher = Pattern.compile(LrtsConfig.REG_BOOK).matcher(data);
		while (matcher.find()) {
			JSONObject bookMap = new JSONObject();
			bookMap.put(LrtsConfig.K_BOOK_ID, matcher.group(1));
			bookMap.put(LrtsConfig.K_PHOTO, matcher.group(2));
			bookMap.put(LrtsConfig.K_NAME, matcher.group(3));
			bookMap.put(LrtsConfig.K_AUTHOR, matcher.group(4));
			bookMap.put(LrtsConfig.K_G_USER, matcher.group(5));
			bookMap.put(LrtsConfig.K_DESC, matcher.group(6));
			bookList.add(bookMap);
		}
		return bookList;
	}

	public static JSONArray seachBook(String world, int page, JSONArray bookList) {
		try {
			world = URLEncoder.encode(world, LrtsConfig.ENCODING);
		} catch (Exception e) {
		}
		String url = String.format("%s/%s/%s/%s", LrtsConfig.URL, LrtsConfig.URL_SEACHBOOK, page, world);
		String data = LrtsUtils.getURLData(url, LrtsConfig.ENCODING);
		if (bookList == null) {
			bookList = new JSONArray();
		}
		Matcher matcher = Pattern.compile(LrtsConfig.REG_BOOK).matcher(data);
		while (matcher.find()) {
			JSONObject bookMap = new JSONObject();
			bookMap.put(LrtsConfig.K_BOOK_ID, matcher.group(1));
			bookMap.put(LrtsConfig.K_PHOTO, matcher.group(2));
			bookMap.put(LrtsConfig.K_NAME, matcher.group(3));
			bookMap.put(LrtsConfig.K_AUTHOR, matcher.group(4));
			bookMap.put(LrtsConfig.K_G_USER, matcher.group(5));
			bookMap.put(LrtsConfig.K_DESC, matcher.group(6));
			bookList.add(bookMap);
		}
		return bookList;
	}

	public static void getBookList(String bookId, int start, JSONObject list) {
		if (list == null)
			return;
		if (!list.containsKey(LrtsConfig.K_CHAPTER_LIST)) {
			list.put(LrtsConfig.K_CHAPTER_LIST, new JSONObject());
		}
		String url = String.format("%s/%s/%s/%s", LrtsConfig.URL, LrtsConfig.URL_PLAYLIST, bookId, start);
		String data = LrtsUtils.getURLData(url, LrtsConfig.ENCODING);
		Matcher matcher = Pattern.compile(LrtsConfig.REG_MP3).matcher(data);
		JSONObject charpterList = list.getJSONObject(LrtsConfig.K_CHAPTER_LIST);
		while (matcher.find()) {
			charpterList.put(String.valueOf(start++), matcher.group(1));
		}
		if ((matcher = Pattern.compile(LrtsConfig.REG_INFO).matcher(data)).find()) {
			list.put(LrtsConfig.K_TYPE, matcher.group(1).trim());
			list.put(LrtsConfig.K_CHAPTER, matcher.group(2).trim());
			list.put(LrtsConfig.K_STATUS, matcher.group(3).trim());
		}
	}


	public static String getURLData(String url, String encoding) {
		try {
			return new String(HTTPUtils.get(url), encoding);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
